package com.ch.dream.logic.longstory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import cn.goso.utility.NumberTools;

import com.ch.dream.R;
import com.ch.dream.activity.longstory.DreamContentActivity;
import com.ch.dream.activity.longstory.DreamListActivity;
import com.ch.dream.asynctask.GetDreamListRequestAsyncTask;
import com.ch.dream.base.BaseLogic;
import com.ch.dream.base.BaseRequestAsyncTask.RequestServerListener;
import com.ch.dream.bean.DreamstoryBean;
import com.ch.dream.tools.ViewTools;

public class DreamListLogic extends BaseLogic {
	
	private ListView lvDreamList;
	private SimpleAdapter adapterMyDream;
	private ProgressDialog loadingDialog;
	private List<HashMap<String, String>> list;
	private String longStoryId;
	public DreamListLogic(DreamListActivity activity) {
		super(activity);
	}

	public DreamListLogic(DreamListActivity activity, int layoutId) {
		super(activity, layoutId);
	}
	
	public void initViews(){
		lvDreamList =(ListView) activity.findViewById(R.id.lvMyDream);
	}
	
	public void initData(){
		longStoryId = activity.getIntent().getStringExtra("longStoryId");
	}
	
	public void init(){
		initViews();
		initData();
		initMystoryList();
	}
	/**
	 * 初始化后续梦境列表
	 */
	public void initMystoryList(){
		DreamstoryBean bean =new DreamstoryBean();
		bean.setLongStoryId(NumberTools.stringToInt(longStoryId));
		new GetDreamListRequestAsyncTask(activity, new RequestServerListener() {
			@Override
			public void onRequestServerEnd(Object result) {
				loadingDialog.dismiss();
				if(result!=null && !result.toString().equals("[]")){
					list = getMystoryList((JSONArray)result);
					adapterMyDream = new SimpleAdapter(activity,list,R.layout.l_dreamlist_item,
							new String[]{"content","isRead","createTime"},new int[]{R.id.tvContent,R.id.tvIsRead,R.id.tvCreateTime}){
						public View getView(int position, View convertView, android.view.ViewGroup parent) {
							convertView = super.getView(position, convertView, parent);
							if(convertView!=null){
								TextView tvContent = (TextView)convertView.findViewById(R.id.tvContent);
								TextView tvIsRead = (TextView)convertView.findViewById(R.id.tvIsRead);
								TextView tvCreateTime = (TextView)convertView.findViewById(R.id.tvCreateTime);
								tvCreateTime.setText(list.get(position).get("createTime").split(" ")[0]);
								tvContent.setText(list.get(position).get("content"));
								
								if("1".equals(list.get(position).get("isRead"))){
									tvIsRead.setText("已阅读");
								}else{
									tvIsRead.setText("未阅读");
								}
							}
							return convertView;
						};
					};
					lvDreamList.setAdapter(adapterMyDream);
					lvDreamList.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> data, View view, int position,
								long id) {
							Intent intent =new Intent(activity,DreamContentActivity.class);
							intent.putExtra("dreamId", list.get(position).get("id"));
							intent.putExtra("longStoryId", list.get(position).get("longStoryId"));
							intent.putExtra("content", list.get(position).get("content"));
							activity.startActivity(intent);
						}
					});
				}else{
					if(result==null){
						ViewTools.showShortToast(activity, "网络错误");
					}else{
						ViewTools.showShortToast(activity, "暂还没有后续梦境");
					}
					
				}
			}
			
			@Override
			public void onRequestServerBegin() {
				loadingDialog = ViewTools.showLoading(activity,"提示", "加载数据中，请稍候...");
			}
		}).execute(bean);
		
	}
	
	/**
	 * 获取后续梦境列表
	 * @param jsonArray
	 * @return
	 */
	public static  List<HashMap<String, String>> getMystoryList(JSONArray jsonArray){
		List<HashMap<String, String>> result= new ArrayList<HashMap<String,String>>();
		try{
			for(int i=0;i<jsonArray.length();i++){
				HashMap<String,String> map = new HashMap<String, String>();
				DreamstoryBean bean = new DreamstoryBean();
				bean.setJsonObject(jsonArray.getJSONObject(i));
				map.put("id",bean.getId().toString());
				map.put("longStoryId",bean.getLongStoryId().toString());
				map.put("content",bean.getContent());
				map.put("createTime",bean.getCreateTime());
				result.add(map);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
}
